import requests
import parsel
import os

url = 'https://b.faloo.com/724903.html'
headers = {
'Cookie':'host4chongzhi=https%3a%2f%2fcn.bing.com%2f; Hm_lvt_6d308f6626f6d0864b6bb4f348f2b5e5=1691131627; nc_rela=1; novelrelative=724903; favorates28=724903%2C1; autobuychapters28=724903%2C1; curr_url=https%3A//b.faloo.com/724903_1.html; Hm_lpvt_6d308f6626f6d0864b6bb4f348f2b5e5=1691131704; readline=1; fontFamily=1; fontsize=16; bgcolor=%23FFFFFE; vip_img_width=3; font_Color=666666',
'User-Agent' : 'Mozilla/5.e (Windons NI 18.8; Winb4;x54)AppleWebkit/537.36(KHIML,like Gecko) Chrome/113.0.0.8 Safari/537.36', 'x-requested-with': 'XMLiIttpHequest',
        } 
response = requests.get(url= url,headers=headers)
#print(response.text)
selector = parsel.Selector(response.text)
#小说名
name = selector.css('#novelName::text').get()
#小说章节名
titles = selector.css('.DivTd a::attr(title)').getall()
#小说章节url
href = selector.css('.DivTd a::attr(href)').getall()
for title,link in zip(titles,href):
    link_url = 'https:' + link
    html_data = requests.get(url=link_url,headers=headers).text
    #6.解析内容
    selector_1 = parsel.Selector(html_data)
    #获取内容
    content_list = selector_1.css('div.noveContent p::text').getall()
    #保存小说（格式)即字符串数据转化
    content = '\n'.join(content_list)
    #保存数据
    with open(name + '.txt',mode='a',encoding='utf-8') as f:
      f.write(title)
      f.write('\n')
      f.write(content)
      f.write('\n')
    file = f'content\\{name}\\'
    if not os.path.exists(file):
       os.makedirs(file)
    with open(file + title.strip() + '.txt',mode='a',encoding='utf-8') as a:
      
      a.write(title)
      a.write('\n')
      a.write(content)
      a.write('\n')


    
    print(content)
    